/**
 * 使用：import {mapState, mapMutitations} from 'vuex'
 *      computed: {
 *         1. ...mapState(['count'])
 *         2. ...mapState({
 *              c : 'count'
 *          })
 *         3. ...mapState('product', ['count'])
 *      }
 *
 *  登录、获取用户信息、退出登录、清除accessToken逻辑，不建议修改
 */
import { isNotBlack } from '../../utils/common'

const state = () => ({
  accessToken: '',
  nickName: '',
  avatar: '',
})

const getters = {
  accessToken: (state) => state.accessToken,
  nickName: (state) => state.nickName,
  avatar: (state) => state.avatar,
}

/**
 * 用来改变数据
 * 每一次改变都可以追踪到，被开发者工具记录下来
 */
const mutations = {

  setUserInfo(state, user = {}) {
    if (isNotBlack(user.accessToken)) {
      state.accessToken = accessToken
    }
    if (isNotBlack(user.nickName)) {
      state.nickName = nickName
    }
    if (isNotBlack(user.avatar)) {
      state.avatar = avatar
    }
  }

}

/*
 * 所有的异步操作  actions -> mutations -> 改变 state
 */
const actions = {

  setVirtualRoles({ commit, dispatch }) {
    dispatch('acl/setFull', true, { root: true })
    commit('setAvatar', 'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif')
    commit('setUsername', 'admin(未开启登录拦截)')
  }

}
export default { state, getters, mutations, actions }
